package de.g18.scavengerhunt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import de.g18.scavengerhunt.AppApplicationContext;
import de.g18.scavengerhunt.AppConstants;
import de.g18.scavengerhunt.R;
import de.g18.scavengerhunt.utils.GameUtils;

/**
 * This activity shows the main menu off the app
 */
public class MainMenuActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	String[] menuItems = { 
    			getString(R.string.theRules), 
    			getString(R.string.createNewGame),
    			getString(R.string.joinGame),
    			getString(R.string.settings),
    			getString(R.string.about),
    			getString(R.string.exitApp)
    	};    	
        setContentView(R.layout.mainmenu);
        
        List<Map<String, String>> items = new ArrayList<Map<String, String>>();
        for (String item : menuItems) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("itemText", item);
            items.add(map);
        }

        // Main menu
        SimpleAdapter sa = new SimpleAdapter(this, items, R.layout.mainmenu_item, new String[] {"itemText"}, new int[] {R.id.itemText});

        ListView listView = (ListView) findViewById(R.id.lstMainMenu);
        listView.setAdapter(sa);

        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                    	GameUtils.showActivity(new RulesActivity());
                        break;
                    case 1:
                        if (AppApplicationContext.getSession() != null) {
                            showDialog(AppConstants.DIALOG_CREATE_GAME_WHILE_INGAME_MESSAGE);
                        } else {
                            GameUtils.showActivity(new NewGameActivity());
                        }
                        break;
                    case 2:
                        GameUtils.showActivity(new GamesActivity());
                        break;
                    case 3:
                        GameUtils.showActivity(new SettingsActivity());
                        break;
                    case 4:
                        GameUtils.showActivity(new AboutActivity());
                        break;
                    case 5:
                        if(AppApplicationContext.getSession() != null) {
                            showDialog(AppConstants.DIALOG_EXIT_PROGRAM_WHILE_INGAME_MESSAGE);
                        } else {
                            exitProgram();
                        }
                        break;
                }
            }
        });
    }

    /**
     * Called automatically when the Activity has been created and will be visible to the user.
     */
    @Override
    protected void onStart() {
    	super.onStart();
    	
    	// Give the ApplicationContext the current activity instance.
    	AppApplicationContext.setActivity(this);
    }
    
    /**
     * Exits (kills) the Program to make sure GPS is not processed anymore.
     */
    private void exitProgram() {
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case AppConstants.DIALOG_CREATE_GAME_WHILE_INGAME_MESSAGE:
                return new AlertDialog.Builder(this).setMessage(getString(R.string.dialogCreateGameWhileInGameMessage))
                        .setTitle(getString(R.string.dialogCreateGameWhileInGameTitle)).setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if(AppApplicationContext.getSession().leave()) {
                                	GameUtils.showActivity(new NewGameActivity());
                                }
                            }
                        }).setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).create();
                
            case AppConstants.DIALOG_EXIT_PROGRAM_WHILE_INGAME_MESSAGE:
                return new AlertDialog.Builder(this).setMessage(getString(R.string.dialogExitProgramWhileInGameMessage))
                        .setTitle(getString(R.string.dialogExitProgramWhileInGameTitle)).setPositiveButton(getString(R.string.exitApp), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                exitProgram();
                            }
                        }).setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).create();
                
        }
        return null;
    }
}